@extends("admin.frame_layout")
<?php $top_selected = "信息管理";$page_menu = "碎片组成" ?>
@section('content')
    <div class="formbody">
        <div class="formtitle"><span>碎片编辑</span></div>

        <form action="{{route('admin.frag.save',['id'=>isset($frag)?$frag->id:0])}}"
              method="post" id="news" enctype="multipart/form-data">
            {!! csrf_field() !!}

            <ul class="forminfo">

                {!! inputText("碎片位置","position","text",
                    isset($frag)?$frag->position:"",
                    "标题建议最长不超过50个字符！") !!}
                {!! inputText("碎片地址or时间","create_addr","text",
                   isset($frag)?$frag->create_addr:"",
                   "地址可以不填写！") !!}

                {{--{!! inputText("碎片标示","slug","text",--}}
                   {{--isset($frag)?$frag->slug:"",--}}
                   {{--"每个碎片只有唯一标示(一旦保存不能修改)！") !!}--}}
                <li>
                    <label class="lable">碎片标示</label>
                    <input id="slug" name="slug" type="text"
                           class="dfinput" value="{{isset($frag)?$frag->slug:''}}"/>
                    <i for="slug">每个碎片只有唯一标示(一旦保存不能修改)！</i>
                </li>
                <li>
                    <label class="lable">碎片图片</label>
                      <input  name="picture" type="file" id="file0"  multiple="multiple" class="dfinput"/>
                        @if(isset($frag)&& $frag->picture != null)
                            <img id="img0" src="{{asset($frag->picture)}}" alt="" width="100" height="100" 	onclick='$("#file0").click()'>
                            <input style="display:none" name="picture" type="file" id="file0"  multiple="multiple" class="dfinput"/>
                        <i>点击图片- 便可更换图片</i>
                        @endif
                </li>

                <li>
                    <label class="lable">碎片标题</label>
                    {{-- 加载编辑器的容器 --}}
                    <div style="float: left;width: 800px;">
                        <script name="title" id="title" type="text/plain">{!! $frag->title or "" !!}</script>
                    </div>
                    <div style="clear: both;"></div>
                </li>



                <li>
                    <label class="lable">碎片内容</label>
                    {{-- 加载编辑器的容器 --}}
                    <div style="float: left;width: 800px;">
                        <script name="content" id="content" type="text/plain">{{$frag->content or "" }}</script>
                    </div>
                    <div style="clear: both;"></div>
                </li>
                <li><label class="lable">&nbsp;</label>
                    <input type="submit" class="btn" value="确认保存"/>
                </li>
            </ul>
        </form>
    </div>
@endsection

@section('footer')
    <script src="{{asset('admin/js/ueditor/ueditor.config.js')}}"></script>
    <script src="{{asset('admin/js/ueditor/ueditor.all.js')}}"></script>
    {{-- 实例化编辑器 --}}
    <script type="text/javascript">
        $("#file0").change(function(){
            var objUrl = getObjectURL(this.files[0]) ;
            console.log("objUrl = "+objUrl) ;
            if (objUrl) {
                $("#img0").attr("src", objUrl) ;
            }
        }) ;
        function getObjectURL(file) {
            var url = null ;
            if (window.createObjectURL!=undefined) { // basic
                url = window.createObjectURL(file) ;
            } else if (window.URL!=undefined) { // mozilla(firefox)
                url = window.URL.createObjectURL(file) ;
            } else if (window.webkitURL!=undefined) { // webkit or chrome
                url = window.webkitURL.createObjectURL(file) ;
            }
            return url ;
        }

        var ue = UE.getEditor('content', {
            initialFrameHeight: 250
        });

        var ti = UE.getEditor('title', {
            initialFrameHeight: 150
        });

        $("#news").validate({

            errorPlacement: function (error, element) {
                $(element).closest("form").find("i[for='" + element.attr("id") + "']").html(error);
            },
            submitHandler: function (form) {
                if (ti.hasContents()) {
                    ajaxSub(form);
                } else {
                    toastr["error"]("请填写碎片标题！");
                }
            }
        });

        /**
         * ajax提交请求
         */
        function ajaxSub(form) {
            $(form).ajaxSubmit({
                dataType: "json",
                type: "post",
                success: function showSuccess(data) {
                    if (data.code == 0) {
                        toastr["success"](data.msg);
                        setInterval(function () {
                            window.location = "{{route('admin.frag')}}";
                        }, 2000);
                    } else {
                        toastr["error"](data.msg);
                    }
                }
            });
        }
    </script>
@endsection
